//
//  RecruitmentViewController.m
//  cityStar
//
//  Created by Hancong on 15/2/11.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "RecruitmentPublishViewController.h"
#import "selectDistrict.h"
#import "RecruitmentViewController.h"

@interface RecruitmentPublishViewController ()<UIPickerViewDelegate,UITextFieldDelegate,UITextViewDelegate>

@property (nonatomic,strong) NSArray *arrayName;
@property (nonatomic,strong) NSArray *arrayid;
@property (nonatomic,strong) NSString *strstartAddressID;
@property (nonatomic,strong) NSString *degreeType;
@property (nonatomic,strong) NSString *salaryType;
@property (nonatomic,assign) int segmentIndex;

@end

@implementation RecruitmentPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setUpViews];
    [self addBackButton];
    [self addRightButtonWithTitle:@"发布须知" actionBlock:^{
    }];
}
#pragma mark - Actions
- (IBAction)seleSegment:(UISegmentedControl *)sender {
    self.segmentIndex = sender.selectedSegmentIndex+1;
}
- (IBAction)btnPublish_Touch:(id)sender {
    NSString *errMsg;
    if (self.fieldTitle.text.length == 0) {
        errMsg = @"请输入标题";
    }else if (self.fieldInstitutionName.text.length == 0){
        errMsg = @"请输入单位名称";
    }else if (self.fieldstation.text.length == 0){
        errMsg = @"请输入职位名称";
    }else if (self.fieldstationAddress.text.length == 0){
        errMsg = @"请输入公司地址";
    }else if (self.fieldRequirement.text.length == 0){
        errMsg = @"请选择学历要求";
    }else if (self.fieldDeadlineTime.text.length == 0){
        errMsg = @"请选择截止日期";
    }else if (self.fieldMailAddress.text.length == 0){
        errMsg = @"请输入邮箱地址";
    }else if (self.fieldName.text.length == 0){
        errMsg = @"请输入联系人";
    }else if (self.fieldPhoneNum.text.length == 0){
        errMsg = @"请输入联系方式";
    }else if (self.fieldWorkAddress.text.length == 0){
        errMsg = @"请选择工作区域";
    }else if (self.fieldWorkRequirement.text.length == 0){
        errMsg = @"请输入工作要求";
    }else if (self.fieldSalary.text.length == 0){
        errMsg = @"请选择薪资待遇";
    }
    if (errMsg) {
        BossSystemShowAlertViewWithTitle(errMsg)
        return;
    }
    
    long time;
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *fromdate=[format dateFromString:self.fieldDeadlineTime.text];
    time = (long)[fromdate timeIntervalSince1970];
    NSString *cityID = [[NSUserDefaults standardUserDefaults]objectForKey:@"city_code"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
    NSDictionary *parameters = @{@"title":self.fieldTitle.text,
                                 @"category":[NSString stringWithFormat:@"%i",self.segmentIndex],
                                 @"company_name":self.fieldInstitutionName.text,
                                 @"career":self.fieldstation.text,
                                 @"degree":self.degreeType,
                                 @"end":[NSString stringWithFormat:@"%li",time],
                                 @"email":self.fieldMailAddress.text,
                                 @"address":self.fieldstationAddress.text,
                                 @"salary":self.salaryType,
                                 @"district":self.strstartAddressID,
                                 @"district_name":self.fieldWorkAddress.text,
                                 @"contact":self.fieldName.text,
                                 @"phone":self.fieldPhoneNum.text,
                                 @"line":self.fieldWorkRequirement.text,
                                 @"city":cityID?cityID:@"122",
                                 };
    [HttpRequestManager postMethod:KInterface_PublishJob parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"发布成功"];
            RecruitmentViewController *recruitmentVC = [[RecruitmentViewController alloc]init];
            [self.navigationController pushViewController:recruitmentVC animated:YES];
        }else{
            [SVProgressHUD showSuccessWithStatus:errMsg];
        }
    }];
}

#pragma mark - setUps
-(void)setUpViews
{
    self.title = @"招聘";
    self.scrollerView.contentSize = CGSizeMake(320, 900);
    self.salaryType = @"1";
    self.degreeType = @"1";
    self.strstartAddressID = [NSString stringWithFormat:@"%@",self.arrayid[0]];

    self.arrayName = [[NSArray alloc]init];
    self.arrayid = [[NSArray alloc]init];
    self.fieldWorkRequirement.placeholder = @"具体工作要求，工作年限，学历等";
    self.fieldWorkRequirement.placeholderColor = [UIColor colorWithWhite:0.797 alpha:1.000];
    self.arrayName  = [selectDistrict shardDistrictInfo].DistrictNameArray;
    self.arrayid =[selectDistrict shardDistrictInfo].DistrictIdArray;
    
    
    UIPickerView *moneyPickerView = [[UIPickerView alloc]init];
    moneyPickerView.delegate = self;
    moneyPickerView.tag = 0;
    self.fieldSalary.inputView = moneyPickerView;
    
    self.fieldDeadlineTime.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    
    UIPickerView *addressPickerView = [[UIPickerView alloc]init];
    addressPickerView.delegate = self;
    addressPickerView.tag = 2;
    self.fieldWorkAddress.inputView = addressPickerView;
    
    UIPickerView *educationPickerView = [[UIPickerView alloc]init];
    educationPickerView.delegate = self;
    educationPickerView.tag = 1;
    self.fieldRequirement.inputView = educationPickerView;
    
    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneButton,nil];
    
    [topView setItems:buttonsArray];
    [self.fieldSalary setInputAccessoryView:topView];
    [self.fieldDeadlineTime setInputAccessoryView:topView];
    [self.fieldWorkAddress setInputAccessoryView:topView];
    [self.fieldRequirement setInputAccessoryView:topView];
    
}

-(void)dismissKeyBoard
{
    [self.fieldSalary resignFirstResponder];
    [self.fieldDeadlineTime resignFirstResponder];
    [self.fieldWorkAddress resignFirstResponder];
    [self.fieldRequirement resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 6:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 50);
                textField.text = @"3000以下";
            }];
        }
            break;
        case 7:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 100);
                textField.text = @"不限";
            }];
        }
            break;
        case 8:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 150);
            }];
            
        }
            break;
        case 9:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 200);
                //                textField.text =  [NSDate date];
            }];
        }
            break;
        case 10:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 250);
            }];
        }
            break;
        case 11:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 300);
            }];
        }
            break;
        case 12:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 350);
                textField.text = self.arrayName[0];
            }];
        }
            break;
            
        default:
            break;
    }
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

#pragma mark - pickerViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (pickerView.tag == 0) {
        return 7;
    }else if(pickerView.tag == 2){
        return self.arrayName.count;
    }else{
        return 8;
    }
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView.tag == 0) {
        if (row == 0) {
            return @"3000 以下";
        }else if (row == 1){
            return @"3000-5000";
        }else if (row == 2){
            return @"5000-8000";
        }else if (row == 3){
            return @"8000-15000";
        }else if (row == 4){
            return @"15000-20000";
        }else if (row == 5){
            return @"20000+";
        }else{
            return @"面议";
        }
    }else if(pickerView.tag == 2){
        return self.arrayName[row];
    }else{
        if (row == 0) {
            return @"不限";
        }else if (row == 1){
            return @"初中";
        }else if (row == 2){
            return @"高中";
        }else if (row == 3){
            return @"中专";
        }else if (row == 4){
            return @"大专";
        }else if (row == 5){
            return @"本科";
        }else if (row == 6){
            return @"硕士";
        }else if (row == 7){
            return @"博士";
        }else{
            return @"留学";
        }
    }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView.tag == 0) {
        if (row == 0) {
            self.fieldSalary.text = @"3000 以下";
        }else if (row == 1){
            self.fieldSalary.text = @"3000-5000";
        }else if (row == 2){
            self.fieldSalary.text = @"5000-8000";
        }else if (row == 3){
            self.fieldSalary.text = @"8000-15000";
        }else if (row == 4){
            self.fieldSalary.text = @"15000-20000";
        }else if (row == 5){
            self.fieldSalary.text = @"20000+";
        }else{
            self.fieldSalary.text =  @" 面议";
        }
        self.salaryType = [NSString stringWithFormat:@"%i",row+1];
    }else if(pickerView.tag == 2){
        NSString *strname = [NSString stringWithFormat:@"%@",self.arrayName[row]];
        self.strstartAddressID = [NSString stringWithFormat:@"%@",self.arrayid[row]];
        self.fieldWorkAddress.text = strname;
    }else{
        if (row == 0) {
            self.fieldRequirement.text = @"不限";
        }else if (row == 1){
            self.fieldRequirement.text = @"初中";
        }else if (row == 2){
            self.fieldRequirement.text = @"高中";
        }else if (row == 3){
            self.fieldRequirement.text = @"中专";
        }else if (row == 4){
            self.fieldRequirement.text = @"大专";
        }else if (row == 5){
            self.fieldRequirement.text = @"本科";
        }else if (row == 6){
            self.fieldRequirement.text = @"硕士";
        }else if (row == 7){
            self.fieldRequirement.text = @"博士";
        }else{
            self.fieldRequirement.text = @"留学";
        }
        self.degreeType = [NSString stringWithFormat:@"%i",row];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
